package models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.jpa.Blob;
import utils.DateHelper;
import utils.ArcadiaConstants.TIPO_PERSONA;

import com.google.gson.annotations.Expose;

import controllers.CRUD.Hidden;

@Entity
public class Organizacion extends ExposedModel {

	public boolean iscliente = false;
	public boolean isproveedor = false;
	public boolean ismedio = false;
	public boolean isintermediario = false;
	public boolean isotro = false;
	
	public TIPO_PERSONA persona;

	public Blob foto;
	
	@Expose
	public String razonSocial = "";
	@Expose
	public String tratamiento;
	@Expose
	public String nombre;
	@Expose
	public String apellido;
	@Expose
	public String marcaComercial;
	
	public String pareja;

	@ManyToOne
	public OrigenDato origenDato;
	
	public String cuit;
	
	public String condicionIva;
	
	public String domicilio;
	
	public String piso;
	
	public String oficina;
	
	@ManyToOne
	public Localidad localidad;

	public String cp;
	
	@MaxSize(1000)
	public String web;
	
	@MaxSize(1000)
	public String mail;
	
	@MaxSize(1000)
	public String telefono;
	
	@MaxSize(1000)
	public String celular;

	public String fax;
	
	@MaxSize(1000)
	public String observaciones;
	
	@ManyToOne	
	public Sector sector;
	@ManyToOne	
	public Rama rama;
	@ManyToOne	
	public Rubro rubro;
	@ManyToOne	
	public Subrubro subrubro;

	@ManyToOne
	public Organizacion intermediario;
	
	@ManyToMany
	public List<ArcadianoNivel> arcadianos;
	
	@Hidden
	public boolean deleted = false;
	
	@Hidden
	@OneToOne
	public ArcadiaLog lastLogs;
	
	public String getActualizado(){
		if (lastLogs!=null)
			return DateHelper.getFechaHora(lastLogs.fecha).concat(" - ").concat(lastLogs.user.username);
		else 
			return "";
	}
	
	public void asociarArcadiano(ArcadianoNivel arcadiano){
		if (arcadianos==null)
			arcadianos = new ArrayList<ArcadianoNivel>();
		arcadianos.add(arcadiano);
	}
	
	public ArcadianoNivel getArcadiano(Long id){
		if (arcadianos!=null)
			for (ArcadianoNivel arcadiano: arcadianos){
				if (arcadiano.arcadiano.id.equals(id))
					return arcadiano;
			}
		return null;
	}
	
	public void removeArcadiano(ArcadianoNivel arcadiano){
		if (arcadianos!=null)
			arcadianos.remove(arcadiano);
	}
	
	@Override
	public String toString() {
		String name = "";
		if (razonSocial!=null && !"".equals(razonSocial.trim()))
			name = name.concat(razonSocial);
		if (nombre!=null && !"".equals(nombre.trim()))
			name = name.concat(" ").concat(nombre);
		if (apellido!=null && !"".equals(apellido.trim()))
			name = name.concat(" ").concat(apellido);
		if (marcaComercial!=null && !"".equals(marcaComercial.trim()))
			name = name.concat(" ").concat(marcaComercial);
		if ("".equals(name.trim()))
			return "Sin Nombre";
		
		return name;
	}
	
	public String arcadianos(){
		String arc = "";
		if (arcadianos!=null){
			for (ArcadianoNivel arcadiano: orderArcadianoNivel(arcadianos)){
				arc = arc.concat(", ");
				arc = arc.concat(arcadiano.arcadiano.nombre);
			}
			arc = arc.replaceFirst(",", "");
		}
		return arc;
	}
	
	private static List<ArcadianoNivel> orderArcadianoNivel(List<ArcadianoNivel> arcadianos) {
		Collections.sort(arcadianos, new Comparator() {

			public int compare(Object o1, Object o2) {
				ArcadianoNivel e1 = (ArcadianoNivel) o1;
				ArcadianoNivel e2 = (ArcadianoNivel) o2;
				return Integer.valueOf(e1.level).compareTo(Integer.valueOf(e2.level));
			}
		});
		return arcadianos;
	}
}
